/**
 * 
 */
package com.khadi.testcase.view;

import info.clearthought.layout.TableLayout;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;

import org.jdesktop.application.Application;

import com.jgoodies.looks.FontPolicies;
import com.jgoodies.looks.plastic.PlasticLookAndFeel;
import com.jgoodies.looks.plastic.PlasticXPLookAndFeel;
import com.jgoodies.looks.plastic.theme.SkyKrupp;
import com.khadi.testcase.view.process.Process;
import com.khadi.testcase.view.process.ProcessGraph;

/**
 * @author m.shinde
 * 
 */
public class UITestCaseApplication extends Application implements
		ActionListener {

	private static final String PROCESS = "PROCESS";
	private static final String PROCESS_GRAPH = "PROCESS_GRAPH";
	private static final String WORK_FLOW = "WORK_FLOW";

	private JDesktopPane desktop;
	private ProcessGraph processGraph;

	public static void main(String[] args) {
		launch(UITestCaseApplication.class, args);
	}

	private IWindowContent localWindowContent;
	private Process process;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jdesktop.application.Application#startup()
	 */
	@Override
	protected void startup() {
		UITestCaseApplication application = new UITestCaseApplication();
		application.prepareFrame();
	}

	public void prepareFrame() {
		try {

			PlasticLookAndFeel.setPlasticTheme(new SkyKrupp());

			UIManager.setLookAndFeel(new PlasticXPLookAndFeel());
			PlasticLookAndFeel.setFontPolicy(FontPolicies
					.getDefaultWindowsPolicy());
			UIManager.put("ScrollBar.is3DEnabled", Boolean.TRUE);

			JFrame.setDefaultLookAndFeelDecorated(true);

			JFrame frame = new JFrame("Test Suite");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

			double[] columns = new double[] { -1 };
			double[] rows = new double[] { -1 };

			TableLayout frameLayout = new TableLayout(columns, rows);
			frameLayout.setHGap(5);
			frameLayout.setVGap(5);

			frame.setLayout(frameLayout);

			desktop = new JDesktopPane();
			desktop.setBackground(Color.WHITE);

			frame.getContentPane().add(desktop, "0,0");
			frame.setJMenuBar(getMenuBar());
			// frame.setBounds(50, 50, 800, 600);
			frame.setPreferredSize(new Dimension(700, 800));
			frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
			frame.pack();
			frame.setVisible(true);

		} catch (Throwable ex) {
			throw new RuntimeException(ex);
		}
	}

	private JMenuBar getMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		JMenu newItem = new JMenu("New");
		JMenuItem newWorkFlowItem = new JMenuItem("Work Flow");

		newWorkFlowItem.addActionListener(this);
		newWorkFlowItem.setActionCommand(WORK_FLOW);

		fileMenu.add(newItem);
		newItem.add(newWorkFlowItem);

		JMenu menu = new JMenu("Tools");
		JMenuItem processMenuItem = new JMenuItem("Process");
		JMenuItem processGraphMenuItem = new JMenuItem("Process Graph");
		processMenuItem.addActionListener(this);
		processMenuItem.setActionCommand(PROCESS);

		processGraphMenuItem.addActionListener(this);
		processGraphMenuItem.setActionCommand(PROCESS_GRAPH);

		menu.add(processMenuItem);
		menu.add(processGraphMenuItem);

		menuBar.add(fileMenu);
		menuBar.add(menu);
		return menuBar;
	}

	private Component getLocalFrame() {
		if (localWindowContent == null) {
			localWindowContent = new TestCaseGeneration();
		}
		return localWindowContent.getView();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			desktop.removeAll();
			if (e.getActionCommand().equals(WORK_FLOW)) {
				JInternalFrame iframe = new JInternalFrame("Test Case ", true,
						true, true);
				iframe.add(getLocalFrame());
				iframe.setVisible(true);
				iframe.pack();
				desktop.add(iframe);
			} else if (e.getActionCommand().equals(PROCESS)) {
				JInternalFrame iframe = new JInternalFrame("Process", true,
						true, true);

				iframe.add(getProcessFrame());

				iframe.setVisible(true);
				iframe.pack();

				desktop.add(iframe);
				iframe.setMaximum(true);
				process.postData(null);
			} else if (e.getActionCommand().equals(PROCESS_GRAPH)) {
				JInternalFrame iframe = new JInternalFrame("Process Graph ",
						true, true, true);

				iframe.add(getProcessGraphFrame());

				iframe.setVisible(true);
				iframe.pack();

				desktop.add(iframe);
				iframe.setMaximum(true);
			}
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	private Component getProcessGraphFrame() {

		if (processGraph == null) {
			processGraph = new ProcessGraph();
		}
		return processGraph.getView();

	}

	private Component getProcessFrame() {

		if (process == null) {
			process = new com.khadi.testcase.view.process.Process();
		}
		return process.getView();  

	}
}